package com.freesky.web.portal.service.impl;


import com.freesky.common.dao.entity.Thanks;
import com.freesky.common.exception.EMallException;
import com.freesky.common.pojo.DataTablesResult;
import com.freesky.web.portal.service.ThanksService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author Freesky
 */
@Service
public class ThanksServiceImpl implements ThanksService {


    @Override
    public DataTablesResult getThanksListByPage(int page, int size) {

        DataTablesResult result=new DataTablesResult();
        if(page<=0) {
            page = 1;
        }
        Page<Thanks> thanksPage = new Thanks().selectPage(new Page<>(page, size), new EntityWrapper<>());
        List<Thanks> list = thanksPage.getRecords();
        if(list==null){
            throw new EMallException("获取捐赠列表失败");
        }


        for(Thanks thanks:list){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = null;
            try {
                date = dateFormat.format(thanks.getDate());
            } catch (Exception e) {
                e.printStackTrace();
            }
            thanks.setTime(date);
            thanks.setEmail(null);
        }

        result.setSuccess(true);
        result.setRecordsTotal(thanksPage.getTotal());
        result.setData(list);
        return result;
    }
}
